package com.api.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.api.dao.ModulesDao;
import com.api.entity.Modules;
import com.api.entity.TreeDto;
import com.api.service.ModulesService;


@Service
public class ModulesServiceImpl implements ModulesService{

	@Autowired
	private ModulesDao mDao;

	public List<Modules> selAll() {
		return mDao.selAll();
	}

	public List<TreeDto> changeTreeNode() {
		//声明树节点集合
		List<TreeDto> listTree = new ArrayList<TreeDto>();
		List<Modules>  listModules = selAll();
		for(Modules modules : listModules) {
			TreeDto tree = new TreeDto();
			tree.setId(modules.getId());
			tree.setTitle(modules.getName());
			tree.setHref(modules.getPath());
			tree.setPid(modules.getParentId());
			listTree.add(tree);
		}
		return listTree;
	}
//加载树形结构
	public List<TreeDto> loadTree() {
		List<TreeDto> TreeNodes = changeTreeNode();
		//声明一个集合存储根节点
		List<TreeDto> rootTree =  new ArrayList<TreeDto>();
		//声明一个集合存储子节点根节点
		for(TreeDto tree:TreeNodes) {
			if(tree.getPid()==0) {
				//父节点等于0  将数据存储到根节点集合
				rootTree.add(getChildren(TreeNodes,tree));
			}
			
		}
		return rootTree;
	}
	//获取对应子节点
	public TreeDto  getChildren(List<TreeDto> rootList,TreeDto dto){
		List<TreeDto> childrenList = new ArrayList<TreeDto>();
		for(TreeDto tree:rootList) {
			if(tree.getPid()==dto.getId()) {
				if(dto.getChildren()==null) {
					dto.setChildren(childrenList);
				}
				dto.getChildren().add(getChildren(rootList,tree));
			}
		}
		return dto;
	}
/**
 * 根据 角色名 获取相应的模块信息
 */
	public List<Modules> findAllByRname(String[] rid) {
		return mDao.findAllByRname(rid);
	}
	public List<TreeDto> changeTreeNodeByRname(String[] rid) {
		//声明树节点集合
		List<TreeDto> listTree = new ArrayList<TreeDto>();
//		List<Modules>  listModules = findAllByRname(rid);
		List<Modules>  listModules = mDao.selAll();
		for(Modules modules : listModules) {
			TreeDto tree = new TreeDto();
			tree.setId(modules.getId());
			tree.setTitle(modules.getName());
			tree.setHref(modules.getPath());
			tree.setPid(modules.getParentId());
			listTree.add(tree);
		}
		return listTree;
	}
//加载树形结构
	public List<TreeDto> loadTreeByRname(String[] rid) {
		List<TreeDto> TreeNodes = changeTreeNodeByRname(rid);
		//声明一个集合存储根节点
		List<TreeDto> rootTree =  new ArrayList<TreeDto>();
		//声明一个集合存储子节点根节点
		for(TreeDto tree:TreeNodes) {
			if(tree.getPid()==0) {
				//父节点等于0  将数据存储到根节点集合
				rootTree.add(getChildren(TreeNodes,tree));
			}
			
		}
		return rootTree;
	}
	//获取对应子节点
	public TreeDto  getChildrenByRid(List<TreeDto> rootList,TreeDto dto){
		List<TreeDto> childrenList = new ArrayList<TreeDto>();
		for(TreeDto tree:rootList) {
			if(tree.getPid()==dto.getId()) {
				if(dto.getChildren()==null) {
					dto.setChildren(childrenList);
				}
				dto.getChildren().add(getChildren(rootList,tree));
			}
		}
		return dto;
	}

	//创建模块
	public int addModules(Modules modules) {
		return mDao.addModules(modules);
	}
	/**
	 * 删除模块
	 */
	public int deleteModules(int mId) {
		return mDao.deleteModules(mId);
	}
	/**
	 * 根据模块id，查询模块信息
	 */
	public Modules findModulesByid(int id) {
		return mDao.findModulesByid(id);
	}
	/**
	 * 更新模块
	 */
	public int updateModulesBymid(Modules modules) {
		return mDao.updateModulesBymid(modules);
	}

}
